from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time, keyboard

import requests
import threading
import json

from config import webdriverPath


def getCookie():
    # 创建ChromeOptions对象
    chrome_options = webdriver.ChromeOptions()
    # 禁止加载图片等资源
    chrome_options.add_argument("--disable-images")
    chrome_options.add_argument("--disable-plugins")
    chrome_options.add_argument("--disable-extensions")
    # 设置全屏参数
    chrome_options.add_argument("--start-maximized")

    # 创建WebDriver对象时传入ChromeOptions
    driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
    driver.get('http://www.scihuber.com/e/member/login/')  # 打开的页面

    # # 登录
    # WebDriverWait(driver, wait_time).until(
    #     EC.visibility_of_element_located((By.ID, 'username'))
    # )
    driver.find_element(By.ID, 'username').send_keys('917612')
    driver.find_element(By.ID, 'password').send_keys('02690773584')
    driver.find_element(By.CSS_SELECTOR, '#maincolumn .lBtn').click()
    # input("请手动操作至论文详情页面,完成后按Enter键继续...")
    time.sleep(5)
    driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=2699')  # 打开的页面

    # 获取所有cookies
    cookies = driver.get_cookies()

    cookieStr = ''
    # 打印cookies
    for cookie in cookies:
        cookieStr = cookieStr + (cookie['name'] + '=' + cookie['value'] + '; ')
    driver.quit()
    # 打开文件
    with open("cookieStr.txt", "w") as file:
        # 写入JSON字符串到文件
        json.dump(cookies, file)
    print(cookieStr)


if __name__ == "__main__":
    getCookie()
